package com.yunze.polling.card;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.yunze.apiCommon.utils.InternalApiRequest;
import com.yunze.common.mapper.mysql.polling.YzSynCardInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 查询卡详情 轮询消费 者
 */
@Slf4j
@Component
public class CardInfoSel {


    @Resource
    private InternalApiRequest internalApiRequest;
    @Resource
    private YzSynCardInfoMapper yzSynCardInfoMapper;




    /**
     *
     * @param msg
     * @param channel_1
     * @throws IOException
     */
    @RabbitHandler
    @RabbitListener(queues = "admin_synCardInfoSel_queue",containerFactory = "customContainerFactory")
    public void cardCardFlow_exchange(String msg, Channel channel_1) {
        if(msg!=null && msg.length()>0){
            synCardFlow(msg);
        }
    }


    /**
     * 死信队列
     * @param msg
     * @param channel_1
     * @throws IOException
     */
    @RabbitHandler
    @RabbitListener(queues = "dlx_admin_synCardInfoSel_queue",containerFactory = "customContainerFactory")
    public void dlxcardCardFlow_exchange(String msg, Channel channel_1)  {
        if(msg!=null && msg.length()>0){
            synCardFlow(msg);
        }
    }



    /**
     * 同步 卡号详情
     *
     * @param msg
     * @throws IOException
     */
    public void synCardFlow(String msg) {
        try {
            if (StringUtils.isEmpty(msg)) {
                return;
            }
            Map<String, Object> pMap = JSON.parseObject(msg);
            Map<String, Object> map = (Map<String, Object>) pMap.get("map");
            Map<String, Object> channelObj = (Map<String, Object>) pMap.get("ChannelObj");
            String msisdn = map.get("msisdn").toString();
            String channel_id = channelObj.get("cd_id").toString();


            Map<String,Object> Rdata = internalApiRequest.queryCardInfo(map,channelObj);//获取卡号详情数据
            String code = Rdata.get("code").toString();
            String cd_code = Rdata.get("cd_code").toString();
            if(code.equals("200")){
                String imsi = null;
                String iccid = null;
                String activeDate = null;
                String openDate = null;
                String remark = null;
                if (cd_code.equals("YiDong_EC") || cd_code.equals("YiDong_EC_TOKE_ShuoLang") || cd_code.equals("YiDong_EC_TengYu") || cd_code.equals("YiDong_EC_Combo") || cd_code.equals("ECV5_token_MW") || cd_code.equals("ECV5_token_IoTLink") ) {
                    Map<String,Object> rMap = (Map<String, Object>) Rdata.get("rMap");
                    imsi = rMap.get("imsi")!=null?rMap.get("imsi").toString():imsi;
                    iccid = rMap.get("iccid")!=null?rMap.get("iccid").toString():iccid;
                    activeDate = rMap.get("activeDate")!=null?rMap.get("activeDate").toString():activeDate;
                    openDate = rMap.get("openDate")!=null?rMap.get("openDate").toString():openDate;
                    remark = rMap.get("remark")!=null?rMap.get("remark").toString():remark;
                }
                Map<String,Object> updMap = new HashMap<>();
                updMap.put("msisdn",msisdn);
                updMap.put("channel_id",channel_id);
                if(imsi!=null){
                    updMap.put("imsi",imsi);
                }
                if(activeDate!=null){
                    updMap.put("activate_date",activeDate);
                }
                if(openDate!=null){
                    updMap.put("open_date",openDate);
                }
                if(remark!=null){
                    updMap.put("remark",remark);
                }
                if(iccid!=null){
                    updMap.put("iccid",iccid);
                    yzSynCardInfoMapper.update(updMap);
                }
            }
        } catch (Exception e) {
            log.error(">>错误 - 同步 卡号详情 消费者:{} , {}<<", e.getMessage(),msg);
        }
    }


}
